/**
 * 
 */
package com.hector.meinewerk.client.view.widget.adapter;

import java.util.ArrayList;

import android.app.Service;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import com.hector.meinewerk.client.R;
import com.hector.meinewerk.common.MeineWerkApplication;
import com.hector.meinewerk.model.Product;

public class ProductAdapter extends BaseAdapter {
	private static final int MIN_ROW = 5;	
	private Context mContext;
	private ArrayList<Product> mProducts = new ArrayList<Product>();

	public ProductAdapter(Context context) {
		mContext = context;
		for(int i = 0 ;i < getItemPerRow()*MIN_ROW; i++){
			mProducts.add(new Product());
		}	
	}

	private int getItemPerRow() {
		return MeineWerkApplication.getWidthScreen()
				/ MeineWerkApplication.convertDensityToPixel(mContext, 90);
	}

	private int getLeftMargin(int positionAtRow) {
		int temp = MeineWerkApplication.getWidthScreen()
				- MeineWerkApplication.convertDensityToPixel(mContext, 90)*getItemPerRow();
		if(positionAtRow == 0){
			return Math.round(temp/(getItemPerRow() + 1) + 0.5f) ;
		}
		
		return temp/(getItemPerRow() + 1);
	}

	@Override
	public int getCount() {
		if(mProducts.size() % getItemPerRow() == 0){
			return mProducts.size() / getItemPerRow();
		}
		return (mProducts.size() / getItemPerRow()) + 1;
	}

	@Override
	public Object getItem(int arg0) {
		if (arg0 < 0 || arg0 > mProducts.size() - 1)
			return null;
		return mProducts.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	static class ViewHolder {
		LinearLayout layoutParentProduct;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder = new ViewHolder();
		if (convertView == null) 
		{
			holder = new ViewHolder();
			LayoutInflater li = (LayoutInflater) mContext
					.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
			convertView = li.inflate(R.layout.item_parent_product, null);
			holder.layoutParentProduct = (LinearLayout) convertView
					.findViewById(R.id.layout_parent_item);

			for (int i = 0; i < getItemPerRow(); i++) {
				final int positionChild = position * getItemPerRow() + i;
				ProductView productView;
				if (mProducts.size() > positionChild && positionChild >= 0) {				
					productView = new ProductView(mContext,
							mProducts.get(positionChild));
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
							LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT);
					params.leftMargin = getLeftMargin(i);
					holder.layoutParentProduct.addView(productView, params);
				}
			}

			convertView.setTag(holder);
		} 
		else {
			holder = (ViewHolder) convertView.getTag();
			for(int i = 0; i < holder.layoutParentProduct.getChildCount(); i++){
				final int positionChild = position * getItemPerRow() + i;
				((ProductView)holder.layoutParentProduct.getChildAt(i)).setProduct(mProducts.get(positionChild));
			}
		}
		
		
		return convertView;
	}

	public ArrayList<Product> getProducts() {
		return mProducts;
	}

	public void setProducts(ArrayList<Product> products) {
		if (products == null)
			return;
		if (products.isEmpty())
			return;

		this.mProducts = products;
		while(this.mProducts.size() < (getItemPerRow()*MIN_ROW)){
			this.mProducts.add(new Product());
		}
		notifyDataSetChanged();
	}

}
